import { Dict } from './data.d';
import { _getDicts } from './service';
import { Effect, Reducer } from "umi"

export type ModelState = {
    list: Dict[];
    item: Dict | {};
    total: number;
}

export type TEffect = Record<'fetchList', Effect>
export type TReducer = Record<'setList' | 'setItem', Reducer<ModelState>>
export default <ModelType<ModelState, TEffect, TReducer>>{
    namespace: 'dict',
    state: {
        list: [],
        item: {},
        total: 0
    },
    effects: {
        *fetchList(_, { put, call }) {
            const res = yield call(_getDicts);
            yield put({
                type: 'setList',
                payload: res.data,
            });
        }
    },
    reducers: {
        setList(state, { list, total }) {
            return {
                ...state,
                list,
                total
            };
        },
        setItem(state) {
            return state;
        }
    }
}